//
//  ZLCollectionView.m
//  ZLCustomViews
//
//  Created by 赵振磊 on 2024/6/19.
//

#import "ZLCollectionView.h"

@interface ZLCollectionView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

@end

@implementation ZLCollectionView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.delegate = self;
    self.dataSource = self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    if (!layout) {
        layout = [[UICollectionViewFlowLayout alloc] init];
    }
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

- (void)reloadData {
    [super reloadData];
    [self scrollViewDidEndScroll];
}

/// 注册cell
- (void)registerCell:(NSArray<NSString *> *)cellNameArray {
    for (NSString *cellName in cellNameArray) {
        Class cellClass = NSClassFromString(cellName);
        if (cellClass) {
            [self registerClass:cellClass forCellWithReuseIdentifier:cellName];
        }
    }
}

- (void)registerCellNib:(NSArray<NSString *> *)nibNameArray {
    for (NSString *nilName in nibNameArray) {
        if (nilName.length > 0) {
            UINib *nib = [UINib nibWithNibName:nilName bundle:[NSBundle mainBundle]];
            if (nib) {
                [self registerNib:nib forCellWithReuseIdentifier:nilName];
            }
        }
    }
}
/// 注册head、footer
- (void)registerHeader:(NSString *)viewClass {
    Class class = NSClassFromString(viewClass);
    if (!class) {
        return;
    }
    [self registerClass:class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:viewClass];
}
/// 注册head、footer
- (void)registerFooter:(NSString *)viewClass {
    Class class = NSClassFromString(viewClass);
    if (!class) {
        return;
    }
    [self registerClass:class forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:viewClass];
}

- (void)registerHeaderNib:(NSString *)nibViewClass {
    if (nibViewClass.length == 0) {
        return;
    }
    UINib *nib = [UINib nibWithNibName:nibViewClass bundle:[NSBundle mainBundle]];
    if (!nib) {
        return;
    }
    [self registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:nibViewClass];
}

- (void)registerFooterNib:(NSString *)nibViewClass {
    if (nibViewClass.length == 0) {
        return;
    }
    UINib *nib = [UINib nibWithNibName:nibViewClass bundle:[NSBundle mainBundle]];
    if (!nib) {
        return;
    }
    [self registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:nibViewClass];
}

#pragma mark - delegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.sectionNumber) {
        return self.sectionNumber();
    }
    return 0;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.itemNumber) {
        return self.itemNumber(section);
    }
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (self.sectionHeaderSize) {
        return self.sectionHeaderSize(section);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (self.sectionFooterSize) {
        return self.sectionFooterSize(section);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.itemSize) {
        return self.itemSize(indexPath);
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (self.rowSpacing) {
        return self.rowSpacing(section);
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (self.colSpacing) {
        return self.colSpacing(section);
    }
    return 0;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.indexPathCell) {
        return self.indexPathCell(collectionView, indexPath);
    }
    return nil;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (self.sectionHeader) {
            return self.sectionHeader(collectionView, indexPath);
        }
        return nil;
    }
    if (self.sectionFooter) {
        return self.sectionFooter(collectionView, indexPath);
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectCell) {
        self.selectCell(indexPath);
    }
}

#pragma mark - scrollview
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 停止类型1、停止类型2
    BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
    if (scrollToScrollStop) {
        [self scrollViewDidEndScroll];
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        // 停止类型3
        BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
        if (dragToDragStop) {
            [self scrollViewDidEndScroll];
        }
    }
}
#pragma mark - scrollView 滚动停止
- (void)scrollViewDidEndScroll {
    if (!self.pageChange || !self.pagingEnabled) {
        return;
    }
    NSIndexPath *indexPath = nil;
    if (self.contentSize.width > self.contentSize.height) {
        CGFloat x = self.contentOffset.x + self.frame.size.width / 2.0;
        indexPath = [self indexPathForItemAtPoint:CGPointMake(x, self.frame.size.height / 2.0)];
    } else {
        CGFloat y = self.contentOffset.y + self.frame.size.height / 2.0;
        indexPath = [self indexPathForItemAtPoint:CGPointMake(self.frame.size.width / 2.0, y)];
    }
    self.pageChange(indexPath);
}

#pragma mark - init

- (void)sectionNumber:(ZLSectionNumber)sectionNumber
           itemNumber:(ZLRowNumber)itemNumber
           rowSpacing:(ZLSpacing)rowSpacing
           colSpacing:(ZLSpacing)colSpacing
    sectionHeaderSize:(ZLHeaderFooterSize)sectionHeaderSize
    sectionFooterSize:(ZLHeaderFooterSize)sectionFooterSize
             itemSize:(ZLItemSize)itemSize
        sectionHeader:(ZLCSectionHeaderFooter)sectionHeader
        sectionFooter:(ZLCSectionHeaderFooter)sectionFooter
        indexPathCell:(ZLIndexPathItem)indexPathCell
           selectCell:(ZLSelectCell)selectCell {
    self.sectionNumber = sectionNumber;
    self.itemNumber = itemNumber;
    self.rowSpacing = rowSpacing;
    self.colSpacing = colSpacing;
    self.sectionHeaderSize = sectionHeaderSize;
    self.sectionFooterSize = sectionFooterSize;
    self.itemSize = itemSize;
    self.sectionHeader = sectionHeader;
    self.sectionFooter = sectionFooter;
    self.indexPathCell = indexPathCell;
    self.selectCell = selectCell;
}

- (void)sectionNumber:(ZLSectionNumber)sectionNumber
           itemNumber:(ZLRowNumber)itemNumber
           rowSpacing:(ZLSpacing)rowSpacing
           colSpacing:(ZLSpacing)colSpacing
             itemSize:(ZLItemSize)itemSize
        indexPathCell:(ZLIndexPathItem)indexPathCell
           selectCell:(ZLSelectCell)selectCell {
    self.sectionNumber = sectionNumber;
    self.itemNumber = itemNumber;
    self.rowSpacing = rowSpacing;
    self.colSpacing = colSpacing;
    self.itemSize = itemSize;
    self.indexPathCell = indexPathCell;
    self.selectCell = selectCell;
}

- (void)sectionNumber:(ZLSectionNumber)sectionNumber
           itemNumber:(ZLRowNumber)itemNumber
             itemSize:(ZLItemSize)itemSize
        indexPathCell:(ZLIndexPathItem)indexPathCell
           selectCell:(ZLSelectCell)selectCell {
    self.sectionNumber = sectionNumber;
    self.itemNumber = itemNumber;
    self.itemSize = itemSize;
    self.indexPathCell = indexPathCell;
    self.selectCell = selectCell;
}

- (void)sectionNumber:(ZLSectionNumber)sectionNumber
           itemNumber:(ZLRowNumber)itemNumber
             itemSize:(ZLItemSize)itemSize
        indexPathCell:(ZLIndexPathItem)indexPathCell {
    self.sectionNumber = sectionNumber;
    self.itemNumber = itemNumber;
    self.itemSize = itemSize;
    self.indexPathCell = indexPathCell;
}

- (void)pageChanged:(ZLPageChange)pageChange {
    self.pageChange = pageChange;
}

@end

#pragma mark - 布局
@interface ZLCircleLayout()
@property (nonatomic, assign) NSInteger count;
@end
@implementation ZLCircleLayout
// 重写父类的四个方法
- (void)prepareLayout {
    self.count = [self.collectionView numberOfItemsInSection:0];
    self.center = CGPointMake(self.collectionView.frame.size.width / 2, self.collectionView.frame.size.height / 2);
    self.radius = 150;
}

// 返回 ContentSize
- (CGSize)collectionViewContentSize {
    return self.collectionView.frame.size;
}

// 对于每一个 Item 都有一个 UICollectionViewLayoutAttributes 对象与它对应，这个对象仅仅包含了Item的布局信息。
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.count; i++) {
        [arr addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]];
    }
    return arr;
}

// 根据 indexPath 来创建 Item 的布局属性对象
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat angle = 2 * M_PI / self.count * indexPath.row + M_PI_2 * self.origin;
    CGFloat x = self.center.x + cos(angle) * self.radius;
    CGFloat y = self.center.y - sin(angle) * self.radius;
    
    attr.center = CGPointMake(x, y);
    attr.size = CGSizeMake(30, 30);
    
    return attr;
}
@end
